<template>
	<!-- <a-layout-header class="header" style="display: inline-block;"> -->
	<a-menu mode="horizontal" style="display: inline-block; line-height: 59px;" :selectedKeys="selectedKeys">
		<a-menu-item v-for="(menu, index) in menus" :key="index" @click="itemClick(index)">
			{{menu.meta.title}}
		</a-menu-item>
	</a-menu>
	<!-- </a-layout-header> -->
</template>

<script>
	import users from "../../store/modules/user.js"
	import eventBus from "../eventBus.js"
	import { USER_AUTH } from "@/store/mutation-types"
	import { postAction } from '@/api/manage.js'
	export default {
		name: "FirstNavgat",
		data() {
			return {
				menus: users.state.menuList,
				selectedKeys: [],
				pageFlag: false,
				timer: null
			}
		},
		methods: {
			itemClick: function(i) {
				this.selectedKeys = [i]
				eventBus.$emit("FirstNavgatClick", i)
			},
			initNavgat: function(path) {
				//处理F5刷新页面后,导航栏对应不上的问题
				if(path.indexOf('bootapi.jeecg') >= 0) {
					this.pageFlag = true
					return
				}
				if(this.pageFlag) {
					this.pageFlag = false
					return
				}
				for (let i = 0; i < this.menus.length; i++) {
					if (path == "/dashboard/analysis") {
						this.selectedKeys = [0]
						eventBus.$emit("FirstNavgatClick", 0)
						return
					} else {
						let childrenData1 = this.menus[i].children
						if (childrenData1 != null && childrenData1.length > 0) {
							for (let j = 0; j < childrenData1.length; j++) {
								if (path == childrenData1[j].path) {
									this.selectedKeys = [i]
									setTimeout(() => {
										eventBus.$emit("FirstNavgatClick", i)
									}, 500)
									return
								} else {
									let childrenData2 = childrenData1[j].children
									if (childrenData2 != null && childrenData2.length > 0) {
										for (let k = 0; k < childrenData2.length; k++) {
											if (path == childrenData2[k].path) {
												this.selectedKeys = [i]
												setTimeout(() => {
													eventBus.$emit("FirstNavgatClick", i)
												}, 500)
												return
											}
										}
									}
								}
							}
						}
					}
				}
			},
			getAlarmPermissions() {
				let auth = sessionStorage.getItem(USER_AUTH);
				if(auth.indexOf("web:user-log:page") >= 0) {
					//开始轮询报警信息定时器
					// this.getAlarmInfo()
					this.timer = window.setInterval(() => {
						this.getAlarmInfo()
					}, 60 * 1000)
				}
			},
			getAlarmInfo() {
				let objTemp = {}
				objTemp.current = 1
				objTemp.size = 10
				objTemp.status = 0
				postAction("/user-log/page", objTemp).then(res => {
					if(res == undefined) {
						window.clearInterval(this.timer)
						return
					}
					if(res.code == '00000') {
						if(res.data.records.length > 0) {
							//提示信息
							this.$notification['warning']({
								message: "有报警信息未处理",
								placement: 'bottomRight'
							})
						}
					}
				})
			}
		},
		created() {
			let path = window.location.pathname
			this.initNavgat(path)
			this.getAlarmPermissions()
		},
		mounted() {
			eventBus.$on("changeNavgat", (path) => {
				this.initNavgat(path)
			})
			eventBus.$on("logout", () => {
				window.clearInterval(this.timer)
			})
		}
	}
</script>

<style>
</style>
